home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / checklpt.arc / CHKLPT.C < prev    next >
Text File  |  1987-04-19  |  9KB  |  195 lines

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /* Program chklpt                                                          */
  4. /*                                                                         */
  5. /* Usage: chklpt PRINTER                                                   */
  6. /*        where PRINTER is (upper, lower or mixed case)                    */
  7. /*           PRN, 1 or LPT1 for LPT1,                                      */
  8. /*           2 or LPT2 for LPT2 or                                         */
  9. /*           3 or LPT3 for LPT3.                                           */
  10. /* Returns ERRORLEVEL   0 if PRINTER is available,                         */
  11. /*                      1 if PRINTER is not available                      */
  12. /*                    100 for certain system errors.                       */
  13. /*                                                                         */
  14. /*    I designed chklpt to tell a batch file whether a printer is          */
  15. /* available.  Unless there is trouble, it should run silently.  In most   */
  16. /* cases of trouble, it gives the user a chance to tell the batch file     */
  17. /* about the printer status manually.                                      */
  18. /*    chklpt requires DOS 2.0 or later to run.                             */
  19. /*    The ERRORLEVEL of 100 occurs only in the event of a compiler or      */
  20. /* system failure.                                                         */
  21. /*                                                                         */
  22. /*    I compiled chklpt and its subfunctions for use with Lattice C,       */
  23. /*  Version 3.  Some of their features which may not be portable are:      */
  24. /*    fileno() is an UNIX function which returns the file handle for a     */
  25. /* file pointer.                                                           */
  26. /*    Lattice classes onerror(action) as a MSDOS function.  onerror(1)     */
  27. /* preeempts the MSDOS critical error handler.  onerror(0) restores the    */
  28. /* MSDOS critical error handler.                                           */
  29. /*    getfc(fh, cw) returns 0 if fh is a valid file handle, -1 otherwise.  */
  30. /* If it is a valid file handle, the function sets cw to the               */
  31. /* word for the file.  Bit 7 is 0 for a disk file.  See DOS interrupt 44H, */
  32. /* AL = 0, for more information about the characteristic word              */
  33. /*    union REGS is a Lattice union which lets the registers become        */
  34. /* arguments to functions which invoke 8086 interrupts.  Two functions     */
  35. /* use it. Both return an int containing the flags after the interrupt.    */
  36. /* intdos invokes interrupt 21H with input registers as the first          */
  37. /* argument and returns the value of the registers after the interrupt as  */
  38. /* the second argument.  int86 invokes the interrupt specified by the      */
  39. /* first argument with input registers as the second argument.  It returns */
  40. /* the value of the registers after the interrupt as the third argument.   */
  41. /*    For simpler interrupt 21H functions, Lattice has a function bdos,    */
  42. /* which takes at least one and up to three arguments.  Its required       */
  43. /* first argument is the function number, which is the input value of AH.  */
  44. /* Its optional second argument is the input value of DX, and its optional */
  45. /* third argument is the input value of AL.  It returns the value of AX    */
  46. /* after the interrupt.                                                    */
  47. /*    yesorno.c uses an IBM ROM BIOS interrupt to handle the keyboard in a */
  48. /* non-rediredctible way.  Some clones may not accept it.                  */
  49. /*                                                                         */
  50. /*    Full identifiers in this program conform to the proposed ANSI        */
  51. /* standard of at most 31 significant characters, and they are different   */
  52. /* within the first 8 characters.  Lattice C includes a -n compiler        */
  53. /* switch which changes the number of significant characters in            */
  54. /* identifiers from 8 to 39.  While this program works without it, I       */
  55. /* designed them to use it.                                                */
  56. /*                                                                         */
  57. /*      This file requires the functions in files CHECKLPT.C and YESORNO.C */
  58. /* to compile and run                                                      */
  59. /*                                                                         */
  60. /* Lew Paper                                                               */
  61. /* 4/17/87                                                                 */
  62. /***************************************************************************/
  63.  
  64. #include <stdio.h>
  65. #include <ctype.h>
  66. #include <string.h>
  67. #include <dos.h>
  68.  
  69. /* Prototypes for external functions                                       */
  70. int FILE_check_lpt_available(FILE *);
  71. int handle_check_lpt_available(int);
  72. int yesorno(char *);
  73.  
  74. /* Prototypes for forward references                                       */
  75. int level2_check_lpt_available(char *);
  76. void usage(void);
  77. void fake_exit(void);
  78.  
  79. #define CHECK_NAME(LEGAL) !strcmpi(argv[1], "LEGAL")
  80. #define CARRY_FLAG 1
  81.  
  82. main(argc, argv)
  83.    int argc;
  84.    char *argv[];
  85.  
  86. {
  87.    int not_available;
  88.  
  89.    /* Check for DOS 2.0 or later                                           */
  90.    if (!(bdos(0x30) & 0xff))
  91.    {
  92.       fputs("CHKLPT requires DOS 2.0 or later.\n\n", stderr);
  93.       fake_exit();
  94.    }
  95.  
  96.    if (argc < 2)
  97.       usage();                         /* Display usage and manually set   */
  98.                                        /* ERRORLEVEL.  Exit from there.    */
  99.  
  100.    /* Check for LPT1 availability with stdprn handle                       */
  101.    if (CHECK_NAME(prn) || CHECK_NAME(1) || CHECK_NAME(lpt1))
  102.       not_available = handle_check_lpt_available(4);
  103.  
  104.    /* Open LPT2 or LPT3 as a level 2 file and check availability with its  */
  105.    /* file pointer                                                         */
  106.    else if (CHECK_NAME(2) || CHECK_NAME(lpt2))
  107.       not_available = level2_check_lpt_available("LPT2");
  108.    else if (CHECK_NAME(3) || CHECK_NAME(lpt3))
  109.       not_available = level2_check_lpt_available("LPT3");
  110.    else
  111.       usage();                         /* Display usage and manually set   */
  112.                                        /* ERRORLEVEL.  Exit from there.    */
  113.  
  114.    exit(not_available);
  115. }
  116.  
  117. int level2_check_lpt_available(device_name)
  118.    char *device_name;
  119.  
  120. {
  121.    FILE *fp;
  122.    int handle;
  123.    int cw;                             /* Characteristic word for file     */
  124.    int not_available;
  125.  
  126.    /*    PC DOS 3.1 opens printers which are off or not installed with     */
  127.    /* no error indication, so I can not test a critical error here.  I     */
  128.    /* preempted the critical error handler because I am not sure that      */
  129.    /* some version of DOS might not treat opening a printer which          */
  130.    /* doesn't exist as a critical error.                                   */
  131.    if (onerror(1))                     /* Preempt MSDOS critical error     */
  132.                                        /* handler.                         */
  133.    {
  134.       fputs("onerror(1) failed in level2_check_lpt_available\n\n", stderr);
  135.       exit(100);
  136.    }
  137.  
  138.    if (!(fp = fopen(device_name, "w")))
  139.    {
  140.       fprintf(stderr, "Unable to open device %s\n\n", device_name);
  141.       fake_exit();
  142.    }
  143.    
  144.    if (getfc((handle = fileno(fp)), &cw) || !(cw & 0x0080))
  145.                                     /* Invalid handle                      */
  146.    {
  147.       fprintf(stderr, "There is no device driver %s installed\n\n",
  148.               device_name);
  149.       fake_exit();
  150.    }
  151.  
  152.    /*    The Lattice C Manual doesn't say exactly how onerror() works.     */
  153.    /* I turned it off here to protect its being turned back on in          */
  154.    /* FILE_check_lpt_available                                             */
  155.    if (onerror(0))                     /* Restore MSDOS critical error     */
  156.    {
  157.       fputs("onerror(0) failed in level2_check_lpt_available\n\n", stderr);
  158.       exit(100);
  159.    }
  160.  
  161.    not_available = handle_check_lpt_available(handle);
  162.  
  163.    if (fclose(fp))
  164.       fprintf(stderr, "\nError in closing device %s\n\n", device_name);
  165.  
  166.    return not_available;
  167.  
  168. }
  169.  
  170. void usage()
  171.  
  172. {
  173.    fputs("Usage: chklpt PRINTER\n", stderr);
  174.    fputs("       where PRINTER is (upper, lower or mixed case)\n", stderr);
  175.    fputs("          PRN, 1 or LPT1 for LPT1,\n", stderr);
  176.    fputs("          2 or LPT2 for LPT2 or\n", stderr);
  177.    fputs("          3 or LPT3 for LPT3.\n", stderr);
  178.    fputs("Returns ERRORLEVEL   0 if PRINTER is available,\n", stderr);
  179.    fputs("                     1 if PRINTER is not available\n", stderr);
  180.    fputs("                   100 for certain system errors\n", stderr);
  181.  
  182.    fake_exit();
  183. }
  184.  
  185. void fake_exit()
  186.  
  187. {
  188.    fputs("\nYou manually determine the ERRORLEVEL for this invocation\n",
  189.           stderr);
  190.    if (yesorno("Do you want to indicate the printer is available (Y or N)?"))
  191.       exit(0);
  192.    else
  193.       exit(1);
  194. }
  195.